package main

import (
	"encoding/json"
	"fmt"
	"log"
)

// rangeAppendElementNotChangeOldSlice
func rangeAppendElementNotChangeOldSlice() {
	s := []int{1, 2, 3}
	for _, v := range s {
		// 在range内append元素不会影响正在range的s的值
		s = append(s, 4)
		log.Print(v) // 1 2 3
	}
	log.Print(s) // 1 2 3 4 4 4
	// 相反的,如果在循环内append会影响len长度，ii永远小于len
	for ii := 0; ii < len(s); ii++ {
		s = append(s, 4)
	}
}

func a() {
	a := []int{1, 2}  //len2 cap2
	b := append(a, 3) //a:len2 cap4 b:len3 cap4
	log.Printf("B:%d,%d,%p,%p", len(b), cap(b), b, a)
	c := append(b, 4) //b:len3 cap4 c:len4 cap4 1234
	log.Printf("C:%d,%d,%p,%p", len(c), cap(c), c, b)
	d := append(b, 5)
	log.Printf("D:%d,%d,%p,%p", len(d), cap(d), d, b)
	fmt.Println(a, b, c[3], d[3])
}

func b() {
	s := []int{1, 2}       //len(2) cap(2)
	s = append(s, 4, 5, 6) //l3 c4 l4c4
	fmt.Println(len(s), cap(s))
}

type AutoGenerated struct {
	Age   int    `json:"age"`
	Name  string `json:"name"`
	Child []int  `json:"child"`
}

func c() {
	jsonStr1 := `{"age": 14,"name": "potter", "child":[1,2,3]}`
	a := AutoGenerated{}
	json.Unmarshal([]byte(jsonStr1), &a)
	aa := a.Child
	fmt.Println(aa)
	jsonStr2 := `{"age": 12,"name": "potter", "child":[3,4,5,7,8,9]}`
	json.Unmarshal([]byte(jsonStr2), &a)
	fmt.Println(aa, a.Child)
}

func d() {
	s := make([]int, 0, 5)
	s = append(s, 1)
	log.Print(s)
}

func e() {
	slice := make([]int, 5, 5)
	change(slice)
	fmt.Printf("%v,%p", slice, &slice)
}

func change(s []int) {
	//s = append(s, 3)
	s = append(s[0:1], 3)
	fmt.Printf("%v,%p", s, &s)
}

func f() {
	var a = [5]int{1, 2, 3, 4, 5}
	var r [5]int

	for i, v := range a {
		if i == 0 {
			a[1] = 12
			a[2] = 13
		}
		r[i] = v
	}
	fmt.Println("r = ", r)
	fmt.Println("a = ", a)
}

func g() {
	var a = []int{1, 2, 3, 4, 5}
	var r [5]int

	for i, v := range a {
		if i == 0 {
			a[1] = 12
			a[2] = 13
		}
		r[i] = v
	}
	fmt.Println("r = ", r)
	fmt.Println("a = ", a)
}

func h() {
	a := [5]int{1, 2, 3}
	b := []int{1, 2, 3}
	c := a
	d := b
	c[0] = 4
	d[0] = 4
	log.Print(a, b)
}

func i() {
	s := make([]int, 0, 10)
	s = append(s, 1, 2, 3)
	b := append(s, 4)
	s = append(s, 5)
	s = append(s, 6)
	log.Print(b)
}

type listNode struct {
	val  int
	next *listNode
}

func printListNode(head *listNode) {
	for head != nil {
		print(head.val)
		head = head.next
	}
}

func reversalListNode(head *listNode) *listNode {
	var pre *listNode
	for head != nil {
		next := head.next
		head.next = pre
		pre = head
		head = next
	}
	return pre
}
func j() {
	head := &listNode{
		val: 1,
		next: &listNode{
			val: 2,
			next: &listNode{
				val:  3,
				next: nil,
			},
		},
	}
	printListNode(head)
	reversalHead := reversalListNode(head)
	printListNode(reversalHead)
}

func merger(s ...[]int) [][]int {
	var ns [][]int
	ns = append(ns, s...)
	return ns
}

func k() {
	s1 := []int{1, 2, 3}
	s2 := []int{1, 2, 3, 4}
	var s [][]int
	var res []int
	s = append(s, s1, s2)

	for {
		n := -1
		for i, ss := range s {
			if len(ss) > 0 {
				if n == -1 || ss[0] < s[n][0] {
					n = i
				}
			}
		}
		if n == -1 {
			break
		}
		res = append(res, s[n][0])
		s[n] = s[n][1:]
	}
}
func A(nums []int) {
	nums[2] = 3
	nums = append(nums, 4)
	//fmt.Println(nums) // 00304
	nums[3] = 3
	//fmt.Println(nums) // 00334

}
func pA() {
	b := make([]int, 4, 4)
	d := b
	A(b)
	fmt.Println(b) //
	fmt.Println(d) //
}
func main() {
}
